package com.wc.算法基础课.C第三讲搜索与图论.DFS.n皇后问题;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/3/14 15:36
 * @description https://www.acwing.com/problem/content/845/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 10;
    static char[][] g = new char[N][N];
    static boolean[] cols = new boolean[N];
    static boolean[] gd = new boolean[N << 1];
    static boolean[] ugd = new boolean[N << 1];
    static int n;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                g[i][j] = '.';
        dfs(0);
        out.flush();
    }

    static void dfs(int u) {
        if (u == n) {
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < n; j++) {
                    out.print(g[i][j]);
                }
                out.println();
            }
            out.println();
            return;
        }
        for (int i = 0; i < n; i++) {
            if (!cols[i] && !gd[u + i] && !ugd[n - i + u]) {
                cols[i] = gd[u + i] = ugd[n - i + u] = true;
                g[u][i] = 'Q';
                dfs(u + 1);
                g[u][i] = '.';
                cols[i] = gd[u + i] = ugd[n - i + u] = false;
            }
        }
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
